﻿using System.Collections.Generic;
using System.ServiceModel;
using PrismTutorial.DataLayer;
using System.Linq;

namespace PrismTutorial.ServiceLibrary {
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class CustomerService : ICustomerService {

        List<CustomerDto> Customers = new List<CustomerDto>();
        IRepository customerRepository = new Repository(new ADVConnection());

        public CustomerService() {
            var result = from c in customerRepository.GetAll<Customer>() orderby c.FirstName, c.LastName select c;
            foreach (var customer in result) {
                Customers.Add(
                    new CustomerDto() { 
                        Id = customer.CustomerID, FullName = customer.FirstName + " " + customer.LastName
                    }
                );
            }
        }

        #region ICustomerService Members

public IList<CustomerDto> GetCustomers() {
    return Customers;
}

public IList<CustomerDto> GetFilteredCustomers(string searchCriteria) {
    return Customers.FindAll(delegate(CustomerDto c) { return c.FullName.ToLower().Contains(searchCriteria.ToLower()); });
}

        #endregion
    }
}
